//
//  HttpRequestManager.m
//  LOLBox
//
//  Created by mac on 15-3-27.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "HttpRequestManager.h"
#import "AFNetworking.h"
#import "NetInterface.h"
#import "GDataXMLNode.h"
#import "DataBaseSImple.h"

//视频模型
#import "YCVideoModel.h"

//新闻资讯的模型
#import "YCNewSModel.h"

//英雄模型
#import "YCHeroModel.h"

#import "YCHeroDetailModel.h"
#import "YCHeroQWERModel.h"

//战斗力查询
#import "YCPowerModel.h"

//装备
#import "YCZBModel.h"

#import "HttpRequest.h"
@interface HttpRequestManager ()

@property (nonatomic, strong) NSMutableArray *VideoListArr;
@property (nonatomic, strong) NSMutableArray *NewArr;
@property (nonatomic, strong) NSMutableArray *seaNewArr;


@property (nonatomic, strong) NSMutableArray *herosArr;

@property (nonatomic, strong) NSMutableArray *loaclArr;


@end

@implementation HttpRequestManager

- (id)init
{
    self = [super init];
    if (self)
    {
        _VideoListArr = [NSMutableArray array];
        _NewArr = [NSMutableArray array];
        _seaNewArr = [NSMutableArray array];
        _herosArr = [NSMutableArray array];
        
        _loaclArr = [NSMutableArray array];
    }
    return self;
}

+ (HttpRequestManager *)shareInstance
{
    static HttpRequestManager *manager;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        manager = [[HttpRequestManager alloc] init];
    });
    return manager;
}
- (void)getVideoList:(NSString *)path andPage:(NSInteger)page withSuccess:(httpRequestSuccess)success andFailure:(httpRequestFailure)failure
{
    //NSLog(@"这的%@---%d",path,page);
    
//    NSMutableArray *arr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:[NSString stringWithFormat:@"%@%d.json",path,page] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSArray *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        //NSLog(@"%@",dict);
        //        if (self.dataSource.count > 0)
        //        {
        //            [self.dataSource removeAllObjects];
        //        }
        [dict enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            //NSLog(@"-------------------");
            //NSLog(@"%@",obj);
            YCVideoModel *m = [[YCVideoModel alloc] init];
            m.Id = obj[@"id"];
            m.img = obj[@"img"];
            m.name = obj[@"name"];
            m.time = obj[@"time"];
            m.length = obj[@"length"];
            m.videoAddr = obj[@"video_addr"];
            m.videoAddrHigh = obj[@"video_addr_high"];
            m.videoAddrSuper = obj[@"video_addr_super"];
            m.youkuId = obj[@"youku_id"];
//            NSLog(@"1----:%@",m.videoAddr);
//            NSLog(@"2----:%@",m.videoAddrHigh);
//            NSLog(@"3----:%@",m.videoAddrSuper);
            [_VideoListArr addObject:m];
        }];
        //成功传这个
        success(self,_VideoListArr);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
        //数据获取失败
        failure(self,error);
    }];
}

- (void)getNewsInfo:(NSString *)path andPage:(NSInteger)page withSuccess:(httpRequestSuccess)success andFailure:(httpRequestFailure)failure
{
    [HttpRequest shareManager];
    [HttpRequest getWithPath:[NSString stringWithFormat:path,page] params:nil success:^(id result) {
        NSArray *arr = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableLeaves error:nil];
        [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            //NSLog(@"-------------------");
            //NSLog(@"%@",obj);
            YCNewSModel *model = [[YCNewSModel alloc] init];
            model.desc = obj[@"desc"];
            model.Id = obj[@"id"];
            model.img = obj[@"img"];
            model.posttime = obj[@"posttime"];
            model.site = obj[@"site"];
            model.thumb_img = obj[@"thumb_img"];
            model.thumb_ok = obj[@"thumb_ok"];
            model.title = obj[@"title"];
            model.topic_id = obj[@"topic_id"];
            
            //NSLog(@"title:%@",model.title);
            [self.NewArr addObject:model];
        }];
        
        success(self,_NewArr);

    } failure:^(NSError *error) {
        
    }];
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//
//    [manager GET:[NSString stringWithFormat:path,page] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSArray *arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
//        [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//            //NSLog(@"-------------------");
//            //NSLog(@"%@",obj);
//            YCNewSModel *model = [[YCNewSModel alloc] init];
//            model.desc = obj[@"desc"];
//            model.Id = obj[@"id"];
//            model.img = obj[@"img"];
//            model.posttime = obj[@"posttime"];
//            model.site = obj[@"site"];
//            model.thumb_img = obj[@"thumb_img"];
//            model.thumb_ok = obj[@"thumb_ok"];
//            model.title = obj[@"title"];
//            model.topic_id = obj[@"topic_id"];
//            
//            //NSLog(@"title:%@",model.title);
//            [self.NewArr addObject:model];
//        }];
//        
//        success(self,_NewArr);
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"失败了");
//        failure(self,error);
//    }];
}

//搜素用
- (void)getSeaNewsInfo:(NSString *)path andPage:(NSInteger)page withSuccess:(httpRequestSuccess)success andFailure:(httpRequestFailure)failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:[NSString stringWithFormat:path,page] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            //NSLog(@"-------------------");
            //NSLog(@"%@",obj);
            YCNewSModel *model = [[YCNewSModel alloc] init];
            model.desc = obj[@"desc"];
            model.Id = obj[@"id"];
            model.img = obj[@"img"];
            model.posttime = obj[@"posttime"];
            model.site = obj[@"site"];
            model.thumb_img = obj[@"thumb_img"];
            model.thumb_ok = obj[@"thumb_ok"];
            model.title = obj[@"title"];
            model.topic_id = obj[@"topic_id"];
            
            //NSLog(@"title:%@",model.title);
            [self.seaNewArr addObject:model];
        }];
        
        success(self,_seaNewArr);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败了");
    }];
}


- (void)getHerosInfo:(NSString *)path andParameters:(NSDictionary *)parameters withSuccess:(httpRequestSuccess)success andFailure:(httpRequestFailure)failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        //NSLog(@"英雄: %@",dict);
        NSArray *heroArr = dict[@"result"];
        //        if (self.dataSource.count > 0)
        //        {
        //            [self.dataSource removeAllObjects];
        //        }
        [heroArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            //NSLog(@"-------------------");
            //NSLog(@"%@",obj);
            YCHeroModel *model = [[YCHeroModel alloc] init];
            model.Id = obj[@"id"];
            model.img = obj[@"img"];
            model.nameC = obj[@"name_c"];
            model.title = obj[@"title"];
            //NSLog(@"title:%@",model.title);
            [_herosArr addObject:model];
            
        
        }];
        //成功传这个
        success(self,_herosArr);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
        //数据获取失败
        failure(self,error);
    }];

}

//解析本地所有英雄
- (void)getLocalHerosInfo:(NSString *)path withSuccess:(localArr)success
{
    NSString *file = [[NSBundle mainBundle] pathForResource:path ofType:@"json"];
//    NSURL *url = [NSURL fileURLWithPath:file];

    NSData *data = [NSData dataWithContentsOfFile:file];
    
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        //NSLog(@"%@",obj);
        YCHeroModel *model = [[YCHeroModel alloc] init];
        model.cnName = obj[@"cnName"];
        model.enName = obj[@"enName"];
        model.attack = obj[@"attack"];
        model.defense = obj[@"defense"];
        model.difficulty = obj[@"difficulty"];
        model.magic = obj[@"magic"];
        model.price1 = obj[@"price1"];
        model.price2 = obj[@"price2"];
        model.seq = obj[@"seq"];
        model.tags = obj[@"tags"];
        model.title = obj[@"title"];
        
        model.img = obj[@"enName"];
        
        [_loaclArr addObject:model];
    }];
    success(_loaclArr);
}

//解析周免  xml
- (void)getLocalFreeHerosInfo:(NSString *)path withSuccess:(localArr)success
{
    NSString *file = [[NSBundle mainBundle] pathForResource:path ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:file];
    
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
    
    NSArray *lists = [document nodesForXPath:@"plist/dict/array/dict" error:nil];
    
    //存英雄数据
    NSMutableArray *heroArr = [NSMutableArray array];
    
    for (GDataXMLElement *element in lists)
    {
        //NSLog(@"=====================");
        NSArray *cnNameArr1 = [element elementsForName:@"key"];
        NSArray *cnNameArr2 = [element elementsForName:@"string"];
        
        //
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        
        for (NSInteger i = 0; i < cnNameArr1.count; i++)
        {
            NSString *str1 = [cnNameArr1[i] stringValue];
            NSString *str2 = [cnNameArr2[i] stringValue];
            [dict setObject:str2 forKey:str1];
        }
        
        [heroArr addObject:dict];
    }
    
    [heroArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        YCHeroModel *model = [[YCHeroModel alloc] init];
        model.cnName = obj[@"cnName"];
        model.enName = obj[@"enName"];
        model.title = obj[@"title"];
        model.img = obj[@"enName"];
        [_loaclArr addObject:model];
    }];
    
    success(_loaclArr);
}

//本地英雄信息详情
- (YCHeroDetailModel *)getLocalHerosInfoWithEnName:(NSString *)enName
{
    NSString *heroName = [NSString stringWithFormat:@"hero_%@",enName];
    NSString *file = [[NSBundle mainBundle] pathForResource:heroName ofType:@"json"];
    //    NSURL *url = [NSURL fileURLWithPath:file];
    
    NSData *data = [NSData dataWithContentsOfFile:file];
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    //NSLog(@"英雄详细:  %@",dict);
    
    YCHeroDetailModel *model = [[YCHeroDetailModel alloc] init];
    model.displayName = dict[@"displayName"];
    model.gameId = dict[@"game_id"];
    model.heroId = dict[@"hero_id"];
    model.Id = dict[@"id"];
    model.keyword = dict[@"keyword"];
    model.name = dict[@"name"];
    model.title = dict[@"title"];
    model.tags = dict[@"tags"];
    model.desc = dict[@"description"];
    model.price = dict[@"price"];
    model.price1 = dict[@"price1"];
    model.price2 = dict[@"price2"];
    model.ratingAttack = dict[@"ratingAttack"];
    model.ratingDefense = dict[@"ratingDefense"];
    model.ratingMagic = dict[@"ratingMagic"];
    model.ratingDifficulty = dict[@"ratingDifficulty"];
    
    NSString *abiNameB = [NSString stringWithFormat:@"%@_B",dict[@"name"]];
    NSString *abiNameQ = [NSString stringWithFormat:@"%@_Q",dict[@"name"]];
    NSString *abiNameW = [NSString stringWithFormat:@"%@_W",dict[@"name"]];
    NSString *abiNameE = [NSString stringWithFormat:@"%@_E",dict[@"name"]];
    NSString *abiNameR = [NSString stringWithFormat:@"%@_R",dict[@"name"]];
    //解析技能
    YCHeroQWERModel *b = [[YCHeroQWERModel alloc] init];
    b.name = [NSString stringWithFormat:@"%@(被动)",dict[abiNameB][@"name"]];
    b.cooldown = dict[abiNameB][@"cooldown"];
    b.range = dict[abiNameB][@"range"];
    b.skillName = dict[abiNameB][@"skill_name"];
    b.desc = dict[abiNameB][@"description"];
    b.cost = dict[abiNameB][@"cost"];
    
    YCHeroQWERModel *q = [[YCHeroQWERModel alloc] init];
    q.name = [NSString stringWithFormat:@"%@(Q)",dict[abiNameQ][@"name"]];
    q.cooldown = dict[abiNameQ][@"cooldown"];
    q.range = dict[abiNameQ][@"range"];
    q.skillName = dict[abiNameQ][@"skill_name"];
    q.desc = dict[abiNameQ][@"description"];
    q.cost = dict[abiNameQ][@"cost"];
    
    YCHeroQWERModel *w = [[YCHeroQWERModel alloc] init];
    w.name = [NSString stringWithFormat:@"%@(W)",dict[abiNameW][@"name"]];
    w.cooldown = dict[abiNameW][@"cooldown"];
    w.range = dict[abiNameW][@"range"];
    w.skillName = dict[abiNameW][@"skill_name"];
    w.desc = dict[abiNameW][@"description"];
    w.cost = dict[abiNameW][@"cost"];
    
    YCHeroQWERModel *e = [[YCHeroQWERModel alloc] init];
    e.name = [NSString stringWithFormat:@"%@(E)",dict[abiNameE][@"name"]];
    e.cooldown = dict[abiNameE][@"cooldown"];
    e.range = dict[abiNameE][@"range"];
    e.skillName = dict[abiNameE][@"skill_name"];
    e.desc = dict[abiNameE][@"description"];
    e.cost = dict[abiNameE][@"cost"];
    
    YCHeroQWERModel *r = [[YCHeroQWERModel alloc] init];
    r.name = [NSString stringWithFormat:@"%@(R)",dict[abiNameR][@"name"]];
    r.cooldown = dict[abiNameR][@"cooldown"];
    r.range = dict[abiNameR][@"range"];
    r.skillName = dict[abiNameR][@"skill_name"];
    r.desc = dict[abiNameR][@"description"];
    r.cost = dict[abiNameE][@"cost"];
    
    model.b = b;
    model.q = q;
    model.w = w;
    model.e = e;
    model.r = r;
    
    return model;
}

//战斗力
- (BOOL)downloadZHLWithName:(NSString *)name withServerName:(NSString *)serName withFName:(NSString *)fName withIsSuccess:(void (^)(BOOL isSi))isSuce
{
    __block BOOL isSuccess = NO;
    
    NSString *s1 = [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *s2 = [serName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //NSString *bb = [s stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *s3 = @"http://115.29.143.211/lol/apiMobile.php?action=getPlayersInfo&target=%@&serverName=%@";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:[NSString stringWithFormat:s3,s1,s2] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        //NSLog(@"打印: %@",dict);
        YCPowerModel *model = [[YCPowerModel alloc] init];
        
        NSDictionary *rank = dict[name][@"ranked"];
        NSDictionary *warzone = dict[name][@"warzone"][@"R_S_5"];
        NSDictionary *zdl = dict[name][@"zdl"];
        NSDictionary *global = dict[name][@"global"];
        NSDictionary *nor = dict[name][@"normal"];
        
        model.rankedDict = rank;
        model.warzoneDict = warzone;
        model.zdl = zdl;
        model.global = global;
        model.normal = nor;
        model.name = name;
        model.zone = fName;
        
        
        //NSLog(@"zz-- %@",warzone);
        if ([responseObject length] > 5)
        {
            [[DataBaseSimple shareInstance] insertIntoDB:model];
            isSuccess = YES;
            isSuce(YES);
        }
        else
        {
            isSuce(NO);
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败了");
        NSLog(@"%@",error);
    }];
    return isSuccess;
}

//装备
- (void)getZBInfo:(void (^)(NSMutableArray *))success
{
    NSMutableArray *dataSource = [NSMutableArray array];
    NSString *file = [[NSBundle mainBundle] pathForResource:@"apiItems" ofType:@"json"];
    //    NSURL *url = [NSURL fileURLWithPath:file];
    
    NSData *data = [NSData dataWithContentsOfFile:file];
    
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        YCZBModel *m = [[YCZBModel alloc] init];
        m.Id = obj[@"id"];
        m.text = obj[@"text"];
        m.nomap = obj[@"nomap"];
        m.maps = obj[@"maps"];
        m.tags = obj[@"tags"];
        [dataSource addObject:m];
    }];
    success(dataSource);
}


@end
